<?php

namespace App\Http\Controllers\Resource;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Remark;
use App\Resource;
use App\Process;
use Auth,Redirect;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Response;

class RemarkController extends Controller
{
    public function index($id){
        $Resource = Resource::find($id);        //获取主表数据

//        $remarks = $Resource->remarks()->get(); //获取关联表数据
        $remarks = DB::table('res_remarks')
            ->where('resources_id','=',$id)
            ->join('processes','process_id','=','processes.id')
            ->join('users','users_id','=','users.id')
            ->select('res_remarks.*','users.name','processes.name as pname')
            ->get();
//        dd($remarks);
        return view('remark.index')->withRemarks($remarks)->withId($id)->withResource($Resource);
    }

    //创建
    public function create($id){
        $process =  Process::where('pid','=','4')->orWhere('pid','=','10')->get()->toArray(); //获取进程列表

        return view('remark.add')->withId($id)->withProcess($process);
    }

    //储存
    public function store(Request $request,$id){

        $uid = Auth::id();                      //获取操作人id
        $Remark = new Remark();                 //对res_remark表进行赋值
        $Remark->created  = time();
        $Remark->des = $request->des;
        $Remark->users_id = $uid;
        $Remark->resources_id = $id;

        $resource = Resource::find($id);

        if($request->yy_process_id == 0){       //判断进程是哪个进程 从而对resources表进行填充
            $Remark->process_id = $request->ms_process_id;
            $resource->ms_process_id = $request->ms_process_id;
            $resource->msyg_id = $uid;
            $resource->process_id = $request->ms_process_id;
        }else{
            $Remark->process_id = $request->yy_process_id;
            $resource->yy_process_id = $request->yy_process_id;
            $resource->yyyg_id = $uid;
            $resource->process_id = $request->yy_process_id;
        }

        $resource->save();

        if($Remark->save()){        //直接放入Remark模型中 走的是 Moedel::fill($fields)->save() 在个方式
            return Redirect::to('/resource');
        }else{
            return Redirect::back()->withInput()->withErrors('添加失败!');
        }
    }

    /*
     * 对于用户的编辑只能修改进程和备注, 其他的字段生成后就不可修改了
     */
    public function edit($rid){

        return view('remark.edit')->withRemark(Remark::find($rid))->withRid($rid);
    }

    //更新
    public function update(Request $request,$id){
        $this->validate($request,[
            'des' => 'max:255',
        ]);
        $data = $request->all();
        $Remark = Remark::find($id);
        $Remark->process_id = $data['process_id'];
        $Remark->des = $data['des'];

        if($Remark->save()){
            return Redirect::to('/resource');
        }else{
            return Redirect::back()->withInput()->withErrors('修改失败!');
        }
    }

    //删除
    public function destroy($id){
        $remark = Remark::findOrFail($id);
        $nid = $remark['resources_id'];
        $remark->delete();
        return redirect('/resource/remark/'.$nid);
    }

    //对邀约的操作
    public function invite($id){

        $process =  Process::where('pid',4)->get()->toArray();                          //获取进程列表
        $process_sub =  Process::where('pid',7)->orWhere('pid',8)->get()->toArray();    //获取进程子列表

        return view('remark.invite')->withId($id)->withProcess($process)->withPro_sub($process_sub);
    }
    //保存邀约的操作
    public function invite_save(Request $request,$id){
        if($request->yy_process_id==0){
            return Redirect::back()->withInput()->withErrors('操作进程不能为空!');
        }

        $uid = Auth::id();                                              //获取操作人id
        $Remark = new Remark();                                         //对res_remark表进行赋值
        $Remark->created  = time();
        $Remark->des = $request->des;
        $Remark->users_id = $uid;
        $Remark->resources_id = $id;
        $Remark->process_id = $request->yy_process_id;

        $resource = Resource::find($id);                                //对resource表进行更新
        $resource->yy_process_id = $request->yy_process_id;
        $resource->yyyg_id = $uid;
        $resource->yyyg_time = time();
        $resource->process_id = $request->yy_process_id;

        //对不同进程的不同处理结果,现在暂时全加上
        $resource->schedule = strtotime($request->schedule);
        $resource->yy_time_again = strtotime($request->yy_time_again);
        $resource->invite_status = $request->yy_process_id;             //这里暂时保留,防止下面执行失败后出现空白
        if(!empty($request->res)){                                      //如果有 再约/失败 的原因 就要重组一下字段了
            $res = explode('_',$request->res);
//            array:2 [
//                      0 => "15"           进程id
//                      1 => "再约B"        进程名,用于写入记录
//                    ]
            $resource->invite_status = $res[0];

            if($request->yy_process_id==7){ //再约的情况
                $Remark->des = '再约原因:'.$res[1].'<br>再约时间'.$request->yy_time_again.'<br>'.$request->des;
            }else{                          //失败的情况
                $Remark->des = '原因:'.$res[1].'<br>'.$request->des;
            }

        }

        if($request->yy_process_id==6){     //邀约成功的情况
            $Remark->des = '预计进店时间'.$request->schedule.'<br>'.$request->des;
        }

        if($Remark->save() && $resource->save()){        //直接放入Remark模型中 走的是 Moedel::fill($fields)->save() 在个方式
            return Redirect::to('/resource/yy_invite/show');
        }else{
            return Redirect::back()->withInput()->withErrors('添加失败!');
        }

    }

    //门市接待列表展示
    public function reception($id){

        $process =  Process::where('pid',10)->get()->toArray();                             //获取进程列表
        $process_sub =  Process::where('pid',12)->orWhere('pid',13)->get()->toArray();      //获取进程子列表

        return view('remark.reception')->withId($id)->withProcess($process)->withPro_sub($process_sub);
    }

    //门市接待结果储存
    public function reception_save(Request $request,$id){
        if($request->yy_process_id==0){
            return Redirect::back()->withInput()->withErrors('操作进程不能为空!');
        }
//        dd($request->all());
        $uid = Auth::id();                      //获取操作人id
        $Remark = new Remark();                 //对res_remark表进行赋值
        $Remark->created  = time();
        $Remark->des = $request->des;
        $Remark->users_id = $uid;
        $Remark->resources_id = $id;
        $Remark->process_id = $request->yy_process_id;

        $resource = Resource::find($id);                             //对resource表进行更新
        $resource->ms_process_id = $request->yy_process_id;
        $resource->msyg_id = $uid;
        $resource->msyg_time = time();
        $resource->process_id = $request->yy_process_id;

        //对不同进程的不同处理结果,现在暂时全加上
        $resource->deal_price = $request->deal_price;               //成交金额
        $resource->order_code = $request->order_code;               //订单号码
        $resource->ms_time_again = strtotime($request->yy_time_again);
        $resource->reception_status = $request->yy_process_id;

        $Remark->des .= '<br>消费意向:'.$request->yixiang;

        if(!empty($request->res)) {                                      //如果有 再约/失败 的原因 就要重组一下字段了
            $res = explode('_', $request->res);
        }

        if($request->yy_process_id==11){//成交的情况
            $Remark->des = '成交价:'.$request->deal_price.';<br>订单号码:'.$request->order_code.'<br>'.$request->des;

            $resource->reception_status = $request->yy_process_id;
            $resource->order_code = $request->order_code;
            $resource->deal_price = $request->deal_price;
        }
        if($request->yy_process_id==12){//未成交
            $Remark->des = '未成交原因:'.$request->$res[1].';<br>消费意向:'.$request->yixiang.';<br>备注:'.$request->des;

            $resource->reception_status = $res[0];
        }
        if($request->yy_process_id==13){//再约
            $Remark->des = '再约原因:'.$request->$res[1].';<br>再约时间:'.$request->yy_time_again.';<br>备注:'.$request->des;

            $resource->reception_status = $res[0];
            $resource->ms_time_again = strtotime($request->yy_time_again);
        }


        if($Remark->save() && $resource->save()){                   //直接放入Remark模型中 走的是 Moedel::fill($fields)->save() 在个方式
            return Redirect::to('/resource/ms_reception');
        }else{
            return Redirect::back()->withInput()->withErrors('添加失败!');
        }
    }


//    ajax请求创建菜单
    public function createSubMenu(Request $request){
        $pid = $request->id;
        $data = Process::where('pid',$pid)->get()->toArray();
        if($data){
            $status = 1;
        }else{
            $status = 0;
        }
        return response()->json(array(
            'status' => $status,
            'msg' => $data,
            'id' => $pid
        ));
//
    }

    //查找自己所属的子类id
//    protected function getSon($data){
//
//    }
}
